#pragma once

#include <winsock2.h>


int DnsQueryExample(void);


enum{
	DNS_WORKER_STATUS_FREE = 0,
	DNS_WORKER_STATUS_QUERY ,
	DNS_WORKER_STATUS_EXISTS,
	DNS_WORKER_STATUS_NOT_EXISTS,


};

typedef struct {  
	unsigned short id;  
	unsigned short flag;  
	unsigned short questions;  
	unsigned short answerRRs;  
	unsigned short authorityRRs;  
	unsigned short additionalRRs;  
} DNS_PKG_HEADER, *DNS_PKG_HEADER_PTR;  

typedef struct {  
	unsigned char * dns_name;  
	unsigned short dns_type;  
	unsigned short dns_class;  
} DNS_PKG_QUERY, *DNS_PKG_QUERY_PTR;  

typedef struct{  
	unsigned short dns_name;  
	unsigned short dns_type;  
	unsigned short dns_class;  
	unsigned short dns_ttl;  
	unsigned char* data;  
}DNS_RESPONSE_ANSWER, *DNS_RESPONSE_ANSWER_PTR;  

class CDnsQueryWorker
{
public:
	CDnsQueryWorker(void);
	~CDnsQueryWorker(void);
	int QueryDns(const char* domain, const char* dns_ip, unsigned short id, unsigned short dns_type = DNS_TYPE_A, u_short dns_port = 53 );

	

protected:

	bool m_bStart;
	bool   m_bQuerying;

	SOCKET sckClient;
	DWORD  dwLast;

	char  m_CacheDomain[512];

	BYTE m_recvBuffer[1024];
	char  m_szIpAddress[64];

	DNS_PKG_HEADER_PTR nphp;  
	DNS_PKG_QUERY_PTR dkqp ;  

	int m_send_size ;
	int m_dns_type;

	int pkgDNS(const char*, DNS_PKG_HEADER_PTR&, DNS_PKG_QUERY_PTR&, unsigned char*,unsigned short);

	bool recvAnalyse(unsigned char* buf, size_t buf_size,    size_t send_size, char* ip) ;

public:
	int CloseWorker(void);
	BOOL OpenWorker(void);
	int Update( DWORD dwTimeout = 200);
	char* GetDomain(void);
	char* GetAddress(void);
};


